package com.sun.controller.front;


import com.sun.pojo.Floor;
import com.sun.pojo.Room;
import com.sun.pojo.RoomType;
import com.sun.service.FloorService;
import com.sun.service.RoomService;
import com.sun.service.RoomTypeService;
import com.sun.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller()
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;
    @Autowired
    private FloorService floorService;
    @Autowired
    private RoomTypeService roomTypeService;

    @RequestMapping("/{id}")
    public String toRoomDetail(@PathVariable("id") int id, Model model) {
        // 获取房间详情信息
        Room room = roomService.findOne(id);
        model.addAttribute("room", room);
        return "front/detail";
    }

    @RequestMapping("/list")
    public String toRoomList(Model model) {
        List<Room> roomList = roomService.findRoomList(null);
        List<Floor> floorList = floorService.initAll();
        List<RoomType> roomTypeList = roomTypeService.initRoomSelectRoomType();

        model.addAttribute("roomList", roomList);
        model.addAttribute("floorList", floorList);
        model.addAttribute("roomTypeList", roomTypeList);
        return "front/roomList";
    }

    @RequestMapping("/list/{roomtypeid}/{floorid}")
    public String toRoomListFilter(@PathVariable("roomtypeid") int roomtypeid, @PathVariable("floorid") int floorid, Model model) {
        List<Floor> floorList = floorService.initAll();
        List<RoomType> roomTypeList = roomTypeService.initRoomSelectRoomType();

        RoomVo roomVo = new RoomVo();
        roomVo.setRoomtypeid(roomtypeid == 0 ? null : roomtypeid);
        roomVo.setFloorid(floorid == 0 ? null : floorid);

        List<Room> roomList = roomService.findRoomList(roomVo);
        model.addAttribute("roomList", roomList);
        model.addAttribute("floorList", floorList);
        model.addAttribute("roomTypeList", roomTypeList);
        model.addAttribute("roomTypeId", roomtypeid);
        model.addAttribute("floorId", floorid);
        return "front/roomList";
    }
}
